package com.gizwits.demo


import org.apache.spark.streaming._
import org.apache.spark.{SparkConf, SparkContext}

object wordcount {


   def main(args: Array[String]) {


     val sparkConf = new SparkConf().setAppName("workCount")
     val sc = new SparkContext(sparkConf)

     val ssc = new StreamingContext(sc, Seconds(1))
     ssc.checkpoint(".")
     val lines = ssc.socketTextStream("localhost", 9999)

     val words = lines.flatMap(_.split(" "))

     val pairs = words.map(word => (word, 1))


     val updateFunc = (values: Seq[Int], state: Option[Int]) => {
       val currentCount = values.sum

       val previousCount = state.getOrElse(0)

       Some(currentCount + previousCount)
     }

     val stateDstream = pairs.updateStateByKey((values: Seq[Int], state: Option[Int]) => {
       val currentCount = values.sum

       val previousCount = state.getOrElse(0)

       Some(currentCount + previousCount)
     })
     stateDstream.print()
     ssc.start()
     ssc.awaitTermination()


   }


   }
